package com.fc.mall.controller;

import com.fc.mall.dto.CommonResult;
import com.fc.mall.dto.HomeStatsInfo;
import com.fc.mall.dto.OrderSalePerDayInfo;
import com.fc.mall.service.SmsHomeStatsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 首页统计数据Controller
 */
@Controller
@Api(tags = "SmsHomeStatsController", description = "首页统计数据")
@RequestMapping("/home/stats")
public class SmsHomeStatsController {
    @Autowired
    private SmsHomeStatsService homeStatsService;

    @ApiOperation("首页统计数据")
    @GetMapping
    @ResponseBody
    public CommonResult getHomeStats() {
        HomeStatsInfo homeStatsInfo = homeStatsService.getHomeStats();
        return new CommonResult().success(homeStatsInfo);
    }

    @ApiOperation("订单指定周期每天数据统计")
    @GetMapping("/ordersaleperday")
    @ResponseBody
    public CommonResult getOrderSalePerDayStats(@RequestParam(value = "start") Date startDate,
                                          @RequestParam(value = "end") Date endDate) {
        List<OrderSalePerDayInfo> result = homeStatsService.getOrderSaleOfPeriod(startDate, endDate);
        return new CommonResult().success(result);
    }

    @InitBinder
    public void initBinder(final WebDataBinder binder){
        final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
